package com.yanqu.road.dao.impl.activity.huashigang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorCellData;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangUserFloor;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class HuaShiGangFloorDaoImpl extends TempDataObjectDao<HuaShiGangUserFloor> {

    public static String getHuaShiGangUserFloorSql = "select * from t_u_huashigang_user_floor where userId=?";

    public Map<Integer, HuaShiGangUserFloor> getHuaShiGangUserFloor(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, HuaShiGangUserFloor> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHuaShiGangUserFloorSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangUserFloor huaShiGangUserFloor = parse(rs);
                map.put(huaShiGangUserFloor.getActivityId(), huaShiGangUserFloor);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHuaShiGangUserFloorSql = "update t_u_huashigang_user_floor set floor=?,cellDataList=?,currentMall=?,nextMall=?," +
            "mallMap=?,gotBigReward=?,mallTimesData=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(HuaShiGangUserFloor huaShiGangUserFloor) {
        huaShiGangUserFloor.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getFloor()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserFloor.getCellDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getCurrentMall()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getNextMall()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangUserFloor.getMallMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER,huaShiGangUserFloor.getGotBigReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangUserFloor.getMallTimesMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserFloor.getUserId()));
        if (getDb().executeNoneQuery(updateHuaShiGangUserFloorSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangUserFloorSql = "insert into t_u_huashigang_user_floor(activityId,userId,floor,cellDataList,currentMall," +
            "nextMall,mallMap,gotBigReward,mallTimesData) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HuaShiGangUserFloor huaShiGangUserFloor) {
        huaShiGangUserFloor.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserFloor.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getFloor()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserFloor.getCellDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getCurrentMall()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getNextMall()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangUserFloor.getMallMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserFloor.getGotBigReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangUserFloor.getMallTimesMapJsonStr()));
        if (getDb().executeNoneQuery(insertHuaShiGangUserFloorSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangUserFloor parseData(ResultSet rs) throws SQLException {
        HuaShiGangUserFloor huaShiGangUserFloor = new HuaShiGangUserFloor();
        huaShiGangUserFloor.setActivityId(rs.getInt("activityId"));
        huaShiGangUserFloor.setUserId(rs.getLong("userId"));
        huaShiGangUserFloor.setFloor(rs.getInt("floor"));
        huaShiGangUserFloor.setCellDataList(JSON.parseObject(rs.getString("cellDataList"), new TypeReference<List<HuaShiGangFloorCellData>>() {
        }));
        huaShiGangUserFloor.setCurrentMall(rs.getInt("currentMall"));
        huaShiGangUserFloor.setNextMall(rs.getInt("nextMall"));
        String mallMapStr = rs.getString("mallMap");
        if (StringUtils.isNullOrEmpty(mallMapStr)) {
            huaShiGangUserFloor.setMallMap(new ConcurrentHashMap<>());
        } else {
            huaShiGangUserFloor.setMallMap(JSON.parseObject(mallMapStr, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }
        huaShiGangUserFloor.setGotBigReward(rs.getInt("gotBigReward"));
        String param = rs.getString("mallTimesData");
        if(StringUtils.isNullOrEmpty(param)){
            param = "{}";
        }
        huaShiGangUserFloor.setMallTimesMap(JSON.parseObject(param, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        return huaShiGangUserFloor;
    }

}
